package com.invest.utils.collection;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Evan
 * @date 2013/8/25
 *
 */
public class CollectionHelper {
	
	public static <S,D> Collection<D> arrToCol(S[] arr,CollectionTransfer<S,D> transfer){
		
		Collection<D> col = Lists.newArrayList();
		if(ArrayUtils.isNotEmpty(arr)){
			for(S item : arr){
				col.add(transfer.doTransfer(item));
			}
		}
		return col;
	} 
	
	public static <S,D> Collection<D> colToCol(Collection<S> list,CollectionTransfer<S,D> transfer){
		
		Collection<D> col = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(list)){
			for(S item : list){
				col.add(transfer.doTransfer(item));
			}
		}
		return col;
	} 
	
	public static <S,D> Collection<D> colToSet(Collection<S> list,CollectionTransfer<S,D> transfer){
		
		Collection<D> col = Sets.newHashSet();
		if(CollectionUtils.isNotEmpty(list)){
			for(S item : list){
				col.add(transfer.doTransfer(item));
			}
		}
		return col;
	} 
	
	public static <S,D> List<D> arrToList(S[] array,CollectionTransfer<S,D> transfer){
		
		List<D> col = Lists.newArrayList();
		if(ArrayUtils.isNotEmpty(array)){
			for(S item : array){
				col.add(transfer.doTransfer(item));
			}
		}
		return col;
	} 
	
	public static <S,D> List<D> colToList(Collection<S> list,CollectionTransfer<S,D> transfer){
		
		List<D> col = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(list)){
			for(S item : list){
				col.add(transfer.doTransfer(item));
			}
		}
		return col;
	} 
	
	public static <T> boolean containsAll(Collection<T> parent,Collection<T> son){
		
		if(CollectionUtils.isEmpty(parent) || CollectionUtils.isEmpty(son)){
			return false;
		}
		for(T s : son){
			if(!parent.contains(s)){
				return false;
			}
		}
		
		return true;
	}
	
	public static <S> String colToString(Collection<S> list,CollectionTransfer<S,String> transfer){
		
		StringBuilder builder = new StringBuilder();
		if(CollectionUtils.isNotEmpty(list)){
			int i=0,end = list.size()-1;
			for(S item : list){
				builder.append(transfer.doTransfer(item));
				if(i < end){
					builder.append(",");
				}
				i++;
			}
		}
		return builder.toString();
		
	}
	

	
	public static final String DEF_SPLITOR = ",";
	public static final String DEF_SUB_SPLITOR = "=";
	
	public static Map<String,String> strToMap(String str,String splitor,String subSplitor){
		
		if(StringUtils.isBlank(str)){
			return null;
		}
		String[] arr = str.split(splitor);
		if(ArrayUtils.isEmpty(arr)){
			return null;
		}
		Map<String,String> map = Maps.newHashMap();
		for(String item : arr){
			if(item.contains(subSplitor)){
				String[] subArr = item.split(subSplitor);
				if(ArrayUtils.isNotEmpty(subArr) && subArr.length == 2){
					map.put(subArr[0], subArr[1]);
				}
			}
		}
		return map;
	}
}
